<?php
namespace haibao\cms\data;

use haibao\cms\model\filter\FeatureSerial as FeatureSerialFilter;
use haibao\frame\data\query\Condition;
use haibao\cms\model\data\FeatureSerial as FeatureSerialData;

class FeatureSerial extends \haibao\cms\data\BaseMysql{
	
	private static $cacheFeatureTreeName = 'FeatureTreeName';
	private static $cacheFeatureListName = 'FeatureListName';
	
	public function __construct(){
		parent::__construct('\haibao\cms\model\data\FeatureSerial');
	}
	
	public function getSerialIdByName($serialName){
	    $sql = 'select Id,SerialName from FeatureSerial where SerialName like %s limit 1';
	    $serialInfo = $this->query($sql,array('%'.$serialName.'%'))->fetch_assoc();
	    return $serialInfo;
	}
	
	public function getMonths($serialId){
		$data = array();
		$serialId = $this->escape($serialId);
		$sql = <<<EOF
	select DISTINCT(DATE_FORMAT(t1.ListTime,'%Y-%m')) as monthStr
	from FeaturePublish t1
	inner join FeatureDefine t2 on t1.FeatureDefineId = t2.Id and t2.SerialId = {$serialId}
	where t1.Status = 2 and ListTime != '0000-00-00 00:00:00' and t1.IsH5=0 and t1.IsList=1
	order by monthStr desc
EOF;
		$result = $this->query($sql);
		while ($result->fetch_assoc()){
			array_push($data,$result->getData('monthStr'));
		}
		
		return $data;
	}

	/**
	 * 修改专题系列
	 * @param int $id
	 * @param array $arrSet
	 */
	public function editSerial($id, $arrSet){
		try{
			$this->beginTranscation();
			$this->updateById($id, $arrSet);
			$this->unsetCache(self::$cacheFeatureTreeName);
			$this->unsetCache(self::$cacheFeatureListName);
			if (isset($arrSet[FeatureSerialFilter::CONDITION_FIELD_NAME_TAGS])){
				$this->editSerialTags($id,$arrSet[FeatureSerialFilter::CONDITION_FIELD_NAME_TAGS],true);		
			}
				
			$this->endTranscation();
		}
		catch (Exception $e){
			$this->rollBack();
			throw $e;
		}
	}
	
	
	
	/**
	 * 添加专题系列
	 * @param \haibao\cms\model\data\FeatureSerial $model
	 */
	public function addSerial($model){
		try{
			$this->beginTranscation();
			$this->add($model);
			$this->unsetCache(self::$cacheFeatureTreeName);
			$this->unsetCache(self::$cacheFeatureListName);
			if ($model->Tags){
				$this->editSerialTags($model->Id,$model->Tags);
			}
			
			$this->endTranscation();
		}
		catch (Exception $e){
			$this->rollBack();
			throw $e;
		}
	}
	
	public function editSerialTags($id,$tagIds,$isEdit = false){
		if ($isEdit){
			$sql = 'delete from FeatureSerialTag where SerialId=%s';
			$this->query($sql,array($id));
		}
			
		if ($tagIds){	
			$sql = 'insert into FeatureSerialTag(SerialId,TagId) values';
			$sqlArr = array();
			$param = array();
			foreach ($tagIds as $tagId){
				array_push($sqlArr,'(%s,%s)');
				array_push($param,$id);
				array_push($param,$tagId);
			}
			$sql .= implode(',',$sqlArr);
			$this->query($sql,$param);
		}
		$this->unsetCache(self::$cacheFeatureTreeName);
		$this->unsetCache(self::$cacheFeatureListName);
	}
	
	/**
	 * 专题树形结构
	 */
	public function getCategoryTree(){
		$categoryTreeCache = $this->getCache(self::$cacheFeatureTreeName);
		$categoryTreeCache = null;
		return $categoryTreeCache ? unserialize($categoryTreeCache) : $this->setCategoryTree();
	}
	
	private function setCategoryTree($return = true){
		$categoryTree = array();
		$categorysDict = $this->getAllCategoryList();
		if($categorysDict){
			foreach($categorysDict as $categoryDict){
			    if($categoryDict['Id'] != 52){ //时装周 系列
			        if(isset($categorysDict[$categoryDict[FeatureSerialFilter::CONDITION_FIELD_NAME_SERIAL_PARENT]])){
			            $categorysDict[$categoryDict[FeatureSerialFilter::CONDITION_FIELD_NAME_SERIAL_PARENT]]['children'][] = &$categorysDict[$categoryDict[FeatureSerialFilter::CONDITION_FIELD_NAME_ID]];
			        }else{
			            $categoryTree[$categoryDict[FeatureSerialFilter::CONDITION_FIELD_NAME_ID]] = &$categorysDict[$categoryDict[FeatureSerialFilter::CONDITION_FIELD_NAME_ID]];
			        }
			    }
			}
		}
		$categoryTree && $this->setCache(self::$cacheFeatureTreeName, serialize($categoryTree));
		if($return){
			return $categoryTree;
		}
	}
	
	public function getAllCategoryList(){
		$categoryListCache = $this->getCache(self::$cacheFeatureListName);
		if(!$categoryListCache){
			$categoryListCache = $this->setAllCategorysDict();
		}
		return $categoryListCache;
	}
	
	private function setAllCategorysDict($return = true){
		$featureSerialFilter = new FeatureSerialFilter();
	
		$featureSerialFilter->where(FeatureSerialFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, FeatureSerialData::STATUS_ENABLE);
		$featureSerialFilter->order(FeatureSerialFilter::CONDITION_FIELD_NAME_SORTINPARENT);
		$categorysDict = $this->getAll($featureSerialFilter);
	
		$categorysArr = array();
		if($categorysDict){
			foreach($categorysDict as  $key=>$categoryModel){
				$categorysArr[$categoryModel->Id] = $categoryModel->toArray();
			}
		}
		if($categorysArr){
			$this->setCache(self::$cacheFeatureListName, $categorysArr);
		}
		if($return){
			return $categorysArr;
		}
	}
	
}